/**
 * 
 */
package com.dalonedrow.engine.ui.base;

import com.dalonedrow.engine.sprite.base.SimplePoint;
import com.dalonedrow.engine.sprite.base.SimpleVector2;
import com.dalonedrow.engine.sprite.base.SpriteBase;
import com.dalonedrow.engine.systems.JOGLErrorHandler;
import com.dalonedrow.engine.systems.base.Action;
import com.dalonedrow.engine.systems.base.InputEvent;
import com.dalonedrow.engine.systems.base.Interface;
import com.dalonedrow.engine.ui.base.panel.Panel;
import com.dalonedrow.engine.ui.base.panel.PortraitPagePanel;
import com.dalonedrow.engine.ui.impl.Button;
import com.dalonedrow.engine.ui.impl.Label;
import com.dalonedrow.engine.ui.impl.UIFactory;
import com.dalonedrow.engine.ui.impl.border.SNESBorder;
import com.dalonedrow.utils.UtilityMethods;


/**
 * @author Donald
 */
public class MessageWindow extends PortraitPagePanel {
	/** the singleton instance. */
	private static MessageWindow	instance;
	/**
	 * Gets the one and only instance of {@link MessageWindow}.
	 * @return {@link MessageWindow}
	 */
	public static MessageWindow getInstance() {
		if (MessageWindow.instance == null) {
			MessageWindow.instance = new MessageWindow();
		}
		return MessageWindow.instance;
	}
	private SpriteBase	backdrop;
	/** the message label. */
	private Label	label;
	/** Hidden constructor. */
	private MessageWindow() {
		super(Panel.PORTRAIT_CENTER);
	}
	private void init() {
		super.setBorder(new SNESBorder());
		label = UIFactory.getInstance().getStandardH1Label("BLAH");
		super.addChild(label);
		Button button = UIFactory.getInstance().getStandardButton("OK");
		button.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				try {
					InputEvent.getInstance().addAction(
							Interface.getInstance(), // object
							Interface.getInstance().getClass().getMethod(
									"hideMessageWindow", null
							),
							null
							);
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
		});
		super.addChild(button);
		super.setVisible(true);
		/*
		backdrop = new BasicSprite() {
			/**
			 * {@inheritDoc}
			 *//*
			@Override
			public void update(final long time) {}
		};
		backdrop.setWidth(
				GlobalConstants.getInstance().getScreenWidth());
		backdrop.setHeight(
				GlobalConstants.getInstance().getScreenHeight());
		backdrop.setPosition(
				0,
				0,
				GlobalConstants.MAX_Z - GlobalConstants.CHILD_STEP
				);
		backdrop.definePrimitive(
				Sprite.TYPE_QUADS,
				new float[] {
						0f, GlobalConsts.getInstance().getScreenHeight(),
						GlobalConsts.getInstance().getScreenWidth(),
						GlobalConsts.getInstance().getScreenHeight(),
						GlobalConsts.getInstance().getScreenWidth(), 0f,
						0f, 0f
						},
						new Color(0, 0, 0,
								(int) (255 * GlobalConsts.FADE_66))
				);
		super.setPreferredSize(0, 0);
		super.getPreferredSize();
		Point pt = GlobalMethods.centerComponentInFrame(this);
		super.setPosition(pt.x, pt.y, GlobalConsts.MAX_Z);
		super.setVisible(true);
		*/
	}
	@Override
	public void prepareForRendering() throws Exception {
		if (label == null) {
			try {
				init();
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
		}
		super.prepareForRendering();
		//Render.getInstance().registerEntity(backdrop);
	}
	/**
	 * Displays a message.
	 * @param text the message
	 */
	public void showMessage(final String text) {
		label.setText(text);
		super.setPreferredSize(0, 0);
		super.getPreferredSize();
		SimplePoint pt = 
				UtilityMethods.getInstance().centerComponentInFrame(this);
		super.setPosition(pt.getX(), pt.getY(), 1000f);
		super.setVisible(true);
	}
}
